﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MG_WIN.Util;
using System.Data.Common;

namespace MG_WIN.DAL
{
    /// <summary>
    /// 数据访问类:PriceSetting
    /// </summary>
    public partial class PriceSetting
    {
        public PriceSetting()
        { }
        #region  Method


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string F01)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_price_setting");
            strSql.Append(" where F01='" + F01 + "' ");
            DbHelper db = new DbHelper();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(MG_WIN.Model.PriceSetting model)
        {
            DbHelper db = new DbHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_price_setting(");
            strSql.Append("F01,F135,F136,F137,F144,F138,F145,F143,F139,F147,F148,F111,F112,F32,create_datetime,update_datetime,ps_discnt_link,ps_price_lvl,ps_retail_price,ps_retail_qty");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + model.upc + "',");
            strSql.Append("" + model.priceMultiple + ",");
            strSql.Append("" + model.price + ",");
            strSql.Append("'" + model.startdate + "',");
            strSql.Append("'" + model.starttime + "',");
            strSql.Append("'" + model.enddate + "',");
            strSql.Append("'" + model.endtime + "',");
            strSql.Append("" + model.packagepricemultiple + ",");
            strSql.Append("" + model.packageprice + ",");
            strSql.Append("" + model.limitedqtyamount + ",");
            strSql.Append("" + model.limitedqtyprice + ",");
            strSql.Append("" + model.discountamount + ",");
            strSql.Append("" + model.discountpercent + ",");
            strSql.Append("" + model.pricemm + ",");
            strSql.Append("'" + model.create_datetime + "',");
            strSql.Append("'" + model.update_datetime + "',");
            strSql.Append("" + model.ps_discnt_link + ",");
            strSql.Append("" + model.ps_price_lvl + ",");
            strSql.Append("" + model.ps_retail_price + ",");
            strSql.Append("" + model.ps_retail_qty + "");
            strSql.Append(")");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(MG_WIN.Model.PriceSetting model)
        {
            DbHelper db = new DbHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_price_setting set ");
            strSql.Append("F135=" + model.priceMultiple + ",");
            strSql.Append("F136=" + model.price + ",");
            strSql.Append("F137='" + model.startdate + "',");
            strSql.Append("F144='" + model.starttime + "',");
            strSql.Append("F138='" + model.enddate + "',");
            strSql.Append("F145='" + model.endtime + "',");
            strSql.Append("F143=" + model.packagepricemultiple + ",");
            strSql.Append("F139=" + model.packageprice + ",");
            strSql.Append("F147=" + model.limitedqtyamount + ",");
            strSql.Append("F148=" + model.limitedqtyprice + ",");
            strSql.Append("F111=" + model.discountamount + ",");
            strSql.Append("F112=" + model.discountpercent + ",");
            strSql.Append("F32=" + model.pricemm + ",");
            strSql.Append("create_datetime='" + model.create_datetime + "',");
            strSql.Append("update_datetime='" + model.update_datetime + "',");
            strSql.Append("ps_discnt_link=" + model.ps_discnt_link + ",");
            strSql.Append("ps_price_lvl=" + model.ps_price_lvl + ",");
            strSql.Append("ps_retail_price=" + model.ps_retail_price + ",");
            strSql.Append("ps_retail_qty=" + model.ps_retail_qty + "");
            strSql.Append(" where F01='" + model.upc + "' ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string F01)
        {
            DbHelper db = new DbHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_price_setting ");
            strSql.Append(" where F01='" + F01 + "' ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            int rows = db.ExecuteNonQuery(dbCommand);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }		/// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string F01list)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_price_setting ");
            strSql.Append(" where F01 in (" + F01list + ")  ");
            DbHelper db = new DbHelper();
            DbCommand dbcommand = db.GetSqlStringCommand(strSql.ToString());
            int rows = db.ExecuteNonQuery(dbcommand);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MG_WIN.Model.PriceSetting GetModel(string F01)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select   ");
            strSql.Append(" F01,F135,F136,F137,F144,F138,F145,F143,F139,F147,F148,F111,F112,F32,create_datetime,update_datetime,ps_discnt_link,ps_price_lvl,ps_retail_price,ps_retail_qty ");
            strSql.Append(" from t_price_setting ");
            strSql.Append(" where F01='" + F01 + "' ");
            DbHelper db = new DbHelper();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            MG_WIN.Model.PriceSetting model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MG_WIN.Model.PriceSetting DataRowToModel(DataRow row)
        {
            MG_WIN.Model.PriceSetting model = new MG_WIN.Model.PriceSetting();
            if (row != null)
            {
                if (row["F01"] != null)
                {
                    model.upc = row["F01"].ToString();
                }
                if (row["F135"] != null && row["F135"].ToString() != "")
                {
                    model.priceMultiple = int.Parse(row["F135"].ToString());
                }
                if (row["F136"] != null && row["F136"].ToString() != "")
                {
                    model.price = double.Parse(row["F136"].ToString());
                }
                if (row["F137"] != null)
                {
                    model.startdate = row["F137"].ToString();
                }
                if (row["F144"] != null)
                {
                    model.starttime = row["F144"].ToString();
                }
                if (row["F138"] != null)
                {
                    model.enddate = row["F138"].ToString();
                }
                if (row["F145"] != null)
                {
                    model.endtime = row["F145"].ToString();
                }
                if (row["F143"] != null && row["F143"].ToString() != "")
                {
                    model.packagepricemultiple = int.Parse(row["F143"].ToString());
                }
                if (row["F139"] != null && row["F139"].ToString() != "")
                {
                    model.packageprice = double.Parse(row["F139"].ToString());
                }
                if (row["F147"] != null && row["F147"].ToString() != "")
                {
                    model.limitedqtyamount = int.Parse(row["F147"].ToString());
                }
                if (row["F148"] != null && row["F148"].ToString() != "")
                {
                    model.limitedqtyprice = double.Parse(row["F148"].ToString());
                }
                if (row["F111"] != null && row["F111"].ToString() != "")
                {
                    model.discountamount = double.Parse(row["F111"].ToString());
                }
                if (row["F112"] != null && row["F112"].ToString() != "")
                {
                    model.discountpercent = double.Parse(row["F112"].ToString());
                }
                if (row["F32"] != null && row["F32"].ToString() != "")
                {
                    model.pricemm = int.Parse(row["F32"].ToString());
                }
                if (row["create_datetime"] != null)
                {
                    model.create_datetime = row["create_datetime"].ToString();
                }
                if (row["update_datetime"] != null)
                {
                    model.update_datetime = row["update_datetime"].ToString();
                }
                if (row["ps_discnt_link"] != null && row["ps_discnt_link"].ToString() != "")
                {
                    model.ps_discnt_link = int.Parse(row["ps_discnt_link"].ToString());
                }
                if (row["ps_price_lvl"] != null && row["ps_price_lvl"].ToString() != "")
                {
                    model.ps_price_lvl = int.Parse(row["ps_price_lvl"].ToString());
                }
                if (row["ps_retail_price"] != null && row["ps_retail_price"].ToString() != "")
                {
                    model.ps_retail_price = double.Parse(row["ps_retail_price"].ToString());
                }
                if (row["ps_retail_qty"] != null && row["ps_retail_qty"].ToString() != "")
                {
                    model.ps_retail_qty = int.Parse(row["ps_retail_qty"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F01,F135,F136,F137,F144,F138,F145,F143,F139,F147,F148,F111,F112,F32,create_datetime,update_datetime,ps_discnt_link,ps_price_lvl,ps_retail_price,ps_retail_qty ");
            strSql.Append(" FROM t_price_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DbHelper db = new DbHelper();
            DbCommand cmd = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(cmd);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" F01,F135,F136,F137,F144,F138,F145,F143,F139,F147,F148,F111,F112,F32,create_datetime,update_datetime,ps_discnt_link,ps_price_lvl,ps_retail_price,ps_retail_qty ");
            strSql.Append(" FROM t_price_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            DbHelper db = new DbHelper();
            DbCommand cmd = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(cmd);
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_price_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DbHelper db = new DbHelper();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            object obj = db.ExecuteScalar(dbCommand);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from t_price_setting T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            DbHelper db = new DbHelper();
            DbCommand cmd = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(cmd);
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "t_price_setting");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "System.Collections.Generic.List`1[Maticsoft.CodeHelper.ColumnInfo]");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<MG_WIN.Model.PriceSetting> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F01,F135,F136,F137,F144,F138,F145,F143,F139,F147,F148,F111,F112,F32,create_datetime,update_datetime,ps_discnt_link,ps_price_lvl,ps_retail_price,ps_retail_qty ");
            strSql.Append(" FROM t_price_setting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<MG_WIN.Model.PriceSetting> list = new List<MG_WIN.Model.PriceSetting>();
            DbHelper db = new DbHelper();
            DbCommand cmd = db.GetSqlStringCommand(strSql.ToString());
            using (IDataReader dataReader = db.ExecuteReader(cmd))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public MG_WIN.Model.PriceSetting ReaderBind(IDataReader dataReader)
        {
            MG_WIN.Model.PriceSetting model = new MG_WIN.Model.PriceSetting();
            object ojb;
            model.upc = dataReader["F01"].ToString();
            model.priceMultiple = Convert.ToInt32(dataReader["F135"]);
            model.price = Convert.ToDouble(dataReader["F136"]);
            model.startdate = dataReader["F137"].ToString();
            model.starttime = dataReader["F144"].ToString();
            model.enddate = dataReader["F138"].ToString();
            model.endtime = dataReader["F145"].ToString();
            model.packagepricemultiple = Convert.ToInt32(dataReader["F143"]);
            model.packageprice = Convert.ToDouble(dataReader["F139"]);
            model.limitedqtyamount = Convert.ToInt32(dataReader["F147"]);
            model.limitedqtyprice = Convert.ToDouble(dataReader["F148"]);
            model.discountamount = Convert.ToDouble(dataReader["F111"]);
            model.discountpercent = Convert.ToDouble(dataReader["F112"]);
            model.pricemm = Convert.ToInt32(dataReader["F32"]);
            model.create_datetime = dataReader["create_datetime"].ToString();
            model.update_datetime = dataReader["update_datetime"].ToString();

            ojb = dataReader["ps_discnt_link"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ps_discnt_link = (int)ojb;
            }
            ojb = dataReader["ps_price_lvl"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ps_price_lvl = (int)ojb;
            }
            ojb = dataReader["ps_retail_price"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ps_retail_price = (double)ojb;
            }
            ojb = dataReader["ps_retail_qty"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ps_retail_qty = (int)ojb;
            }
            return model;
        }

        #endregion  Method
    }
}